﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// 分布列表
    /// GridView扩展用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PaginatedList<T> : List<T>
    {

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public string SortType { get; private set; }
        public string OrderByColumnName { get; private set; }

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="source"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sort"></param>
        /// <param name="orderby"></param>
        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize, string sort, string orderby)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            SortType = sort;
            OrderByColumnName = orderby;

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        } 
        #endregion

        /// <summary>
        /// 有下一页
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        /// <summary>
        /// 有上一页
        /// </summary>
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }
}